package tu.java.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import tu.java.db.DBConnManager;

public class MovieImage {
	
	
	
	private int		movieImageId;
	private int		movieId;
	private byte[]	movieImage;
	
	
	
	public boolean addMovieImage() {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement("INSERT INTO TM03_MOVIE_IMAGES (CM01_MOVIE_ID,CM03_MOVIE_IMAGE) VALUES(?,?)");
			pstmt.setInt(1, movieId);
			pstmt.setBytes(2, movieImage);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public boolean deleteMovieImage() {
		
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement("DELETE FROM TM03_MOVIE_IMAGES WHERE CM03_MOVIE_IMAGE_ID=?");
			pstmt.setInt(1, movieImageId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public static Vector<MovieImage> loadAllMovieImages(int movieId) {
		Vector<MovieImage> movieImages = new Vector<MovieImage>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement("SELECT CM03_MOVIE_IMAGE_ID,CM01_MOVIE_ID,CM03_MOVIE_IMAGE FROM TM03_MOVIE_IMAGES WHERE CM01_MOVIE_ID=?");
			pstmt.setInt(1, movieId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MovieImage mi = new MovieImage();
				mi.movieImageId = rs.getInt(1);
				mi.movieId = rs.getInt(2);
				mi.movieImage = rs.getBytes(3);
				movieImages.add(mi);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return movieImages;
	}
	
	
	
	public int getMovieImageId() {
		return movieImageId;
	}
	
	
	
	public void setMovieImageId(int movieImageId) {
		this.movieImageId = movieImageId;
	}
	
	
	
	public int getMovieId() {
		return movieId;
	}
	
	
	
	public void setMovieId(int movieId) {
		this.movieId = movieId;
	}
	
	
	
	public byte[] getMovieImage() {
		return movieImage;
	}
	
	
	
	public void setMovieImage(byte[] movieImage) {
		this.movieImage = movieImage;
	}
	
}
